package com.jd.fruit.controller.mobile;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.jd.fruit.common.annotation.Controller;
import com.jd.fruit.common.controller.BaseController;
import com.jd.fruit.exception.CartException;
import com.jd.fruit.model.admin.order.OrderUser;
import com.jd.fruit.model.admin.order.ShoppingCart;

@Controller("/mobile/cart")
public class CartController extends BaseController{

	CartEntity cartEntity = new CartEntity();
	
	public void index(){
		cartEntity.initByCookie(getRequest());
		this.setAttr("count", cartEntity.getCheckedItemCount());
		this.setAttr("sum", cartEntity.getCheckedItemSum());
		this.setAttr("items", cartEntity.getItemsAsJson(true));
		this.render("/mobile/cart/cart_index.html");
	}
	
	public void view(){
		cartEntity.initByCookie(getRequest());
		this.setAttr("count", cartEntity.getItemCount());
		this.setAttr("sum", cartEntity.getItemSum());
		this.setAttr("items", cartEntity.getItemsAsJson(true));
		this.render("/mobile/cart/cart_view.html");
	}
	
	public void checkout(){
		cartEntity.initByCookie(getRequest());
		this.setAttr("checkedItems", cartEntity.getCheckedItems(true));
		this.setAttr("itemCount",cartEntity.getCheckedItemCount());
		this.setAttr("itemSum",cartEntity.getCheckedItemSum());
		this.render("/mobile/cart/cart_checkout.html");
	}
	
	public void getCheckedItemCount(){
		cartEntity.initByCookie(getRequest());
		int count = cartEntity.getCheckedItemCount();
		this.renderJson("{count:"+count+"}");
	}
	
	public void cartPay(){
		try {
			cartEntity.cartPay(getRequest(), "14cf744fb52045968b46e3036889139e");
		} catch (CartException e) {
			e.printStackTrace();
			this.renderJson("{success:false,msg:'"+e.getMessage()+"'}");
			return;
		}
		this.renderJson("{success:true}");
	}
	
	public void store(){
		cartEntity.initByCookie(getRequest());
		OrderUser orderUser = new OrderUser();
		orderUser.set("id", "1");
		cartEntity.persist(this.getResponse());
		this.render("/mobile/cart/cart_store.html");
	}
	
	public void list(){
		ShoppingCart cart = new ShoppingCart();
		cart.set("user_id", "1");
		List<ShoppingCart> carts =  cart.list();
		this.setAttr("items", carts);
		this.render("/mobile/cart/cart_list.html");
	}
	
	
	public void addCartItem(){
		String id = this.getPara("goodsId");
		Integer count = this.getParaToInt("num");
		try {
			cartEntity.addItem(id, count,this.getRequest(),this.getResponse());
		} catch (RuntimeException e) {
			e.printStackTrace();
			this.renderJson("{success:false,msg:'"+e.getMessage()+"'}");
			return;
		}
		this.renderJson("{success:true}");
	}
	
	public void removeItem(){
		String goddsSkuId = this.getPara("goddsSkuId");
		String cartId = this.getPara("cartId");
		try {
			cartEntity.removeItem(goddsSkuId,cartId,this.getRequest(),this.getResponse());
		} catch (RuntimeException e) {
			e.printStackTrace();
			this.renderJson("{success:false,msg:'"+e.getMessage()+"'}");
		}
		this.renderJson("{success:true}");
	}
	
	@SuppressWarnings("unchecked")
	public void updateItemChecked(){
		try {
			String cartId = this.getPara("id");
			Integer isChecked = this.getParaToInt("is_checked");
			cartEntity.updateItemChecked(cartId, isChecked);
		} catch (RuntimeException e) {
			e.printStackTrace();
			this.renderJson("{success:false,msg:'"+e.getMessage()+"'}");
		}
		this.renderJson("{success:true}");
	}
	
	public void updateNum(){
		String id = this.getPara("id");
		Integer count = this.getParaToInt("num");
		try {
			cartEntity.updateNum(id, count,this.getRequest(),this.getResponse());
		} catch (RuntimeException e) {
			e.printStackTrace();
			this.renderJson("{success:false,msg:'"+e.getMessage()+"'}");
		}
		this.renderJson("{success:true}");
	}
	
	public void cartInfo(){
		cartEntity.initByCookie(getRequest());
		this.renderJson(cartEntity.getItemsAsJson().toJSONString());
	}
	
	public void cartCollectInfo(){
		cartEntity.initByCookie(getRequest());
		int num = cartEntity.getCheckedItemCount();
		double sum = cartEntity.getCheckedItemSum();
		JSONObject obj = new JSONObject();
		obj.put("count", num);
		obj.put("sum", sum);
		this.renderJson(obj.toJSONString());
	}
	
	public String viewCart(){
		cartEntity.initByCookie(getRequest());
		this.setAttr("count", cartEntity.getItemCount());
		this.setAttr("sum", cartEntity.getItemSum());
		return "front/cart";
	}
	
	
}
